<?php

require_once dirname(__FILE__).'/../bootstrap.php';
require_once dirname(__FILE__).'/../../lib/model/doctrine/PluginurCronItem.class.php';

clear_database();
$t = new lime_test(23, new lime_output_color());

$urCronFeed = new urCronFeed();
$urCronFeed->setSiteUrl('http://foo.bar');
$urCronFeed->setFeedUri('http://foo.bar/foobar.xml');
$urCronFeed->setClass('urFooBar');
$urCronFeed->save();
$data = array('original_id'                 => 'http://foo.bar/Z34xB234b312',
              'original_content'            => "foo bar foo bar foo bar foo bar",
              'original_title'              => "FOO BAR!",
              'title'                       => "FOO BAR!",
              'original_summary'            => "",
              'is_published'                => true,
              'original_link'               => "http://foo.bar/foobar",
              'original_published_date'     => date('Y-m-d H:i:s'),
              'ur_cron_feed_id'             => $urCronFeed->getId(),
              'original_author'             => "Marc"
             );

$item = new urCronItem();

$t->is($item->__toString(), 'urCronItem', '->toString() returns the right name when no title is provided');
$item->synchronizeWithArray($data);
try
{
  $item->save();
  $t->pass('->save() save');
}
catch(Exception $e)
{
  $t->fail('->save() save. '.$e->getMessage());
}

$t->is($item->__toString(), 'FOO BAR!', '->toString() returns the right name when title is provided');
$t->is($item->getUrCronFeed() instanceof urCronFeed, true, '->getUrCronFeed() returns an instance of urCronFeed');
$t->is($item->getFeedSiteUrl(), 'http://foo.bar', '->getFeedSiteUrl() returns the correct feed uri');
$t->is($item->getFeedClassName(), 'urFooBar', '->getFeedClassName() returns the correct class name');

// TAGS
$t->diag('Tags');
$item->addTag('foobar')->save();
$t->isnt(Doctrine::getTable('urCronTag')->findOneByName('foobar'), null, '->addTag() insert the tag into database');
$t->is($item->getTags()->count(), 1, '->addTag() link the tag');
$t->is($item->getTags()->getFirst()->getName(), 'foobar', '->addTag() tag name is valid');

try {
  $item->save();
  $t->pass('->save() without any other changes works.');
}
catch(Exception $e)
{
  $t->fail('->save() without any other changes works.\n'.$e->getMessage().'\n'.$e->getTraceAsString());
}

// ->removeTags()
$item->removeTags();
$t->is($item->getTags()->count(), 0, '->removeTags() removes all tags');

$item->addTag('foobar')->addTag('foobar')->addTag('foobar')->save();
$t->is($item->getTags()->count(), 1, "->addTag() skip already added and not saved tag");
$item->removeTags();

$item->addTag('')->save();
$t->is($item->getTags()->count(), 0, '->addTag() do not add empty tags (\'\')');
$item->removeTags();

$item->addTag('   ')->save();
$tags = $item->getTags();
$t->is($tags->count(), 0, '->addTag() do not add tag made of spaces (\'    \')');

$item->addTag('   abce    ')->save();
$tags = $item->getTags();
$t->is($tags->getFirst()->getName(), 'abce', '->addTag() trim whitespaces before and after text (\'   abce   \')');
$item->removeTags();

$tag = new urCronTag();
$tag->setName('megafoobar')->save();
$item->addTag('megafoobar')->save();
$t->is($item->getTags()->getFirst()->getId(), $tag->getId(), "->addTag() link to existing tag in database instead of creating new one");


$t->diag('Datas');
// DATAS
try {
  $item->addData('foo', 'bar')->save();
  $t->pass('->addData() save simple data');
}
catch(Exception $e)
{
  $t->fail("->addData() save simple data\n".$e->getMessage()."\n".$e->getTraceAsString());
}

$item->removeDatas();
$t->is($item->getDatas()->count(), 0, '->removeDatas() unlink item from existing datas');

$item->addData('foo', 'bar')->addData('foo', 'bar')->save();
$t->is($item->getDatas()->count(), 1, "->addData() do not duplicate datas");
$item->removeDatas();

$item->addData('foo', 'bar')->addData('foo', 'barbar')->save();
$t->is($item->getDatas()->getFirst()->getValue(), 'barbar', '->addData() value is replaced by the newest one');
$item->removeDatas();

$item->addData('foo', 'bar')->addData('bar', 'foo')->save();
$t->is($item->getDatas()->count(), 2, '->addData() added multiples datas');
$item->removeDatas();

$item->addData('', 'bar')->save();
$t->is($item->getDatas()->count(), 0, '->addData() doesn\'t add empty keys');
$item->removeDatas();

$item->addData('foo', '')->save();
$t->is($item->getDatas()->getFirst()->getValue(), '', '->addData() add key with empty value');

